package com.example.himalaya.presenters;

import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.data.SubDao;
import com.example.himalaya.data.ISubCallback;
import com.example.himalaya.interfaces.ISubscriptionPresenter;
import com.example.himalaya.interfaces.ISubscriptionViewCallback;
import com.example.himalaya.utils.Constants;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class SubPresenter implements ISubscriptionPresenter, ISubCallback {

    private final SubDao mSubDao;
    private Map<Long, Album> mData = new HashMap<>();
    private List<ISubscriptionViewCallback> mCallbacks = new ArrayList<>();

    private SubPresenter() {
        mSubDao = SubDao.getSubDao();
        mSubDao.setCallback(this);
    }

    private static SubPresenter sSubPresenter;

    public static SubPresenter getSubPresenter() {
        if (sSubPresenter==null) {
            synchronized (SubPresenter.class) {
                if (sSubPresenter==null) {
                    sSubPresenter = new SubPresenter();
                }
            }
        }
        return sSubPresenter;
    }

    private void getSubListData() {
        //可观察的被订阅内容
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mSubDao != null) {
                    mSubDao.getSubList();
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    @Override
    public void registerViewCallBack(ISubscriptionViewCallback iSubscriptionViewCallback) {
        if (!mCallbacks.contains(iSubscriptionViewCallback)) {
            mCallbacks.add(iSubscriptionViewCallback);
        }
    }

    @Override
    public void unRegisterViewCallBack(ISubscriptionViewCallback iSubscriptionViewCallback) {
        mCallbacks.remove(iSubscriptionViewCallback);
    }

    @Override
    public void addSub(Album album) {

        //判断当前的订阅数量，不能超过100个
        if (mData.size()>= Constants.MAX_SUB_COUNT) {
            if (mCallbacks != null) {
                for (ISubscriptionViewCallback callback : mCallbacks) {
                    callback.onSubFull();
                }
            }
            return;
        }


        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mSubDao != null) {
                    mSubDao.addSub(album);
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅
    }

    @Override
    public void deleteSub(Album album) {
        //可观察的
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> emitter) throws Exception {
                if (mSubDao != null) {
                    mSubDao.delSub(album);
                }
            }
        }).subscribeOn(Schedulers.io()).subscribe();//在io线程里进行订阅

    }

    @Override
    public void getSubList() {
        getSubListData();
    }

    @Override
    public boolean isSub(Album album) {
        Album result = mData.get(album.getId());
        //不为空，表示以订阅
        return result != null;
    }





    @Override
    public void onAddSub(boolean isSuccess) {
        getSubListData();
        BaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                for (ISubscriptionViewCallback callback : mCallbacks) {
                    callback.onAddSub(isSuccess);
                }
            }
        });
    }

    @Override
    public void onDelSub(boolean isSuccess) {
        getSubListData();
        BaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                for (ISubscriptionViewCallback callback : mCallbacks) {
                    callback.onDeleteSub(isSuccess);
                }
            }
        });

    }

    @Override
    public void onGetSubList(List<Album> albums) {
        mData.clear();
        for (Album album : albums) {
            mData.put(album.getId(), album);
        }

        BaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                for (ISubscriptionViewCallback callback : mCallbacks) {
                    callback.onGetSubList(albums);
                }
            }
        });


    }
}
